﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Brewmaster
{
    public partial class MenuRadioButton : UserControl
    {

        private const int MENU_BUTTON_HEIGHT = 10;

        public bool Checked
        {
            get
            {
                return radioButton.Checked;
            }
            set
            {
                radioButton.Checked = value;
            }
        }

        public ContextMenuStrip MenuStrip
        {
            get
            {
                return menuButton.MenuStrip;
            }
            set
            {
                menuButton.MenuStrip = value;
            }
        }

        public Image Image
        {
            get
            {
                return radioButton.Image;
            }
            set
            {
                radioButton.Image = value;
            }
        }

        public ContentAlignment ImageAlign
        {
            get
            {
                return radioButton.ImageAlign;
            }
            set
            {
                radioButton.ImageAlign = value;
            }
        }

        public TextImageRelation TextImageRelation
        {
            get
            {
                return radioButton.TextImageRelation;
            }
            set
            {
                radioButton.TextImageRelation = value;
            }
        }

        public ContentAlignment TextAlign
        {
            get
            {
                return radioButton.TextAlign;
            }
            set
            {
                radioButton.TextAlign = value;
            }
        }

        public String Label
        {
            get
            {
                return radioButton.Text;
            }
            set
            {
                radioButton.Text = value;
            }
        }
        
        public MenuRadioButton()
        {
            InitializeComponent();
            this.Load += new EventHandler(MenuRadioButton_Load);
            radioButton.CheckedChanged += new EventHandler(MenuRadioButton_CheckedChanged);
            radioButton.MouseMove += new MouseEventHandler(MenuRadioButton_MouseMove);
            radioButton.MouseHover += new EventHandler(radioButton_MouseHover);
            this.ParentChanged += new EventHandler(MenuRadioButton_ParentChanged);
            this.Resize += new EventHandler(MenuRadioButton_Resize);
            radioButton.KeyPress += new KeyPressEventHandler(MenuRadioButton_KeyPress);
            radioButton.Click += new EventHandler(radioButton_Click);
            radioButton.MouseDown += new MouseEventHandler(radioButton_MouseDown);
            menuButton.MouseDown += new MouseEventHandler(radioButton_MouseDown);
            menuButton.MouseLeave += new EventHandler(menuButton_MouseLeave);
        }

        void menuButton_MouseLeave(object sender, EventArgs e)
        {
            if (radioButton.Checked == false)
            {
                menuButton.Visible = false;
            }
        }

        void radioButton_Click(object sender, EventArgs e)
        {
            if (radioButton.Checked == false) // remove this condition to get the control to act as a checkbox
            {
                radioButton.Checked = !radioButton.Checked;
            }
        }

        void MenuRadioButton_Load(object sender, EventArgs e)
        {
            checkCheckedness(true);
        }

        public void checkCheckedness(bool force = false)
        {
            if (radioButton.Checked == true)
            {
                if (Parent != null)
                {
                    foreach (Control control in Parent.Controls)
                    {
                        if (control != this && control.GetType() == this.GetType())
                        {
                            ((MenuRadioButton)control).Checked = false;
                        }
                    }
                }
                if ((menuButton.Visible == false) || force)
                {
                    setMenuButtonVisibility(true);
                }
            }
            else
            {
                if ((menuButton.Visible == true) || force)
                {
                    setMenuButtonVisibility(false);
                }
            }
        }

        void radioButton_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                radioButton.MouseDown -= radioButton_MouseDown;
                menuButton.MouseDown -= radioButton_MouseDown;
                radioButton.MouseUp += new MouseEventHandler(radioButton_MouseUp);
                menuButton.MouseUp += new MouseEventHandler(radioButton_MouseUp);
                menuButton.PerformClick();
            }
        }

        void radioButton_MouseUp(object sender, MouseEventArgs e)
        {
            radioButton.MouseUp -= radioButton_MouseUp;
            menuButton.MouseUp -= radioButton_MouseUp;
            radioButton.MouseDown += new MouseEventHandler(radioButton_MouseDown);
            menuButton.MouseDown += new MouseEventHandler(radioButton_MouseDown);
        }

        void MenuRadioButton_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Down)
            {
                menuButton.PerformClick();
            }
        }

        void MenuRadioButton_ParentChanged(object sender, EventArgs e)
        {
            if (this.Parent != null)
            {
                this.Parent.MouseMove += new MouseEventHandler(MenuRadioButton_MouseLeave);
                if (this.Parent.Parent == null)
                {
                    this.Parent.ParentChanged += new EventHandler(Parent_ParentChanged);
                }
                else
                {
                    this.Parent.Parent.MouseMove += new MouseEventHandler(MenuRadioButton_MouseLeave);
                }
            }
        }

        void Parent_ParentChanged(object sender, EventArgs e)
        {
            if (this.Parent.Parent != null)
            {
                this.Parent.Parent.MouseMove += new MouseEventHandler(MenuRadioButton_MouseLeave);
            }
        }

        void setMenuButtonVisibility(bool visibility)
        {
            if (visibility == true)
            {
                menuButton.Visible = true;
                menuButton.Top = this.Height - menuButton.Height;
            }
            else
            {
                menuButton.Visible = false;
            }
            this.Refresh();
        }

        void manageMenuButtonVisibility()
        {
            if (!radioButton.Checked && !menuButton.Visible)
            {
                setMenuButtonVisibility(true);
            }
        }

        void MenuRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            checkCheckedness();
        }

        void MenuRadioButton_MouseMove(object sender, MouseEventArgs e)
        {
            manageMenuButtonVisibility();
            if (Parent != null)
            {
                foreach (Control control in Parent.Controls)
                {
                    if (control != this && control.GetType() == this.GetType())
                    {
                        ((MenuRadioButton)control).checkCheckedness();
                    }
                }
            }
        }

        void radioButton_MouseHover(object sender, EventArgs e)
        {
            manageMenuButtonVisibility();
        }

        void MenuRadioButton_MouseLeave(object sender, MouseEventArgs e)
        {
            checkCheckedness();
        }

        void MenuRadioButton_Resize(object sender, EventArgs e)
        {
            radioButton.Left = 0;
            menuButton.Left = 0;
            menuButton.Height = MENU_BUTTON_HEIGHT;
            radioButton.Height = this.Height;
            radioButton.Width = this.Width;
            menuButton.Width = this.Width;
            checkCheckedness(true);
        }
    }
}
